package com.heyu.cgsd.ui.adapter;

import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public abstract class BaseRVAdapter<T> extends RecyclerView.Adapter<BaseRVAdapter.VH> {
    private List<T> mData;

    private OnItemClickListener onItemClickListener;

    public BaseRVAdapter(List<T> data) {
        this.mData = data;
    }

    public abstract int getLayoutId(int viewType);

    public abstract void convert(VH holder, T data, int position);

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
//        void onItemLongClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        return VH.get(viewGroup, getLayoutId(viewType));
    }

    @Override
    public void onBindViewHolder(@NonNull final VH vh, final int position) {
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    int pos = vh.getLayoutPosition();
                    onItemClickListener.onItemClick(vh.itemView, pos);
                }
            }
        });
        convert(vh, mData.get(position), position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    // 删除数据
    public void removeData(int position) {
        mData.remove(position);
        //删除动画
        notifyItemRemoved(position);
        notifyDataSetChanged();
    }

    // 添加数据
    public void addData(int position) {
        //添加动画
        notifyItemInserted(position);
        notifyItemRangeChanged(position, mData.size() - position);//通知数据与界面重新绑定
    }

    protected static class VH extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;
        private View mConvertView;

        private VH(View itemView) {
            super(itemView);

            mConvertView = itemView;
            mViews = new SparseArray<>();
        }

        private static VH get(ViewGroup parent, int layoutId) {
            View convertView = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
            return new VH(convertView);
        }

        public <T extends View> T getView(int id) {
            View v = mViews.get(id);
            if (v == null) {
                v = mConvertView.findViewById(id);
                mViews.put(id, v);
            }
            return (T) v;
        }

        public void setText(int id, String value) {
            TextView tv = getView(id);
            tv.setText(value);
        }

        public void setImage(int id, int resourceId) {
            ImageView iv = getView(id);
            iv.setImageResource(resourceId);
        }

        public void setImage(int id, Bitmap bm) {
            ImageView iv = getView(id);
            iv.setImageBitmap(bm);
        }
    }

    public void updateData(List<T> list) {
        mData = list;
        notifyDataSetChanged();
    }
}
